<template>
  <Card title="新建特殊供应商">
    <SupplierTabsForm
      ref="supplier-form"
      type="special"
      :list="supplierInfoList"
      :data="supplierData"
      :rules="rules" />
    <div class="actions">
      <el-button :loading="loading" @click="checkValidity" type="primary">确认提交</el-button>
      <!-- <el-button :loading="loading" @click="handleBack()">取消</el-button> -->
    </div>
    <el-dialog width="720px"
      :title="'提交审核'"
      :visible="dialogVisible"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form>
        <el-form-item label="原因说明" label-width="100px">
          <el-input
            type="textarea"
            v-model="causeDescription"
            :placeholder="`请输入原因说明`"
            maxlength="50"
            :autosize="{ minRows: 10}"
            :show-word-limit="true" />
          <p>注：原因说明长度在50个字以内；</p>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">提交审核</el-button>
        <el-button @click="dialogVisible=false">取消</el-button>
      </div>
    </el-dialog>
  </Card>
</template>
<script>
import SupplierTabsForm from '@/components/supplier/SupplierTabsForm';
import {
  addSpecialSupplierBillService
} from '@/api/supplier/management/specialSupplier';
import Dict from '@/mixins/supplier/dict';
import ManageArea from '@/mixins/supplier/manageArea';
import { getInitialFieldsValue } from '@/pages/supplier/utils'
import {
  PRODUCT_INFORMATION_FIELDS,
  MANAGEMENT_INFORMATION_FIELDS,
  BILLING_INFORMATION_FIELDS,
  BILLING_METHOD_FIELDS,
  FINANCIAL_INFORMATION_FIELDS,
  DELIVERY_INFORMATION_FIELDS,
} from './constants/info'
export default {
  mixins: [Dict, ManageArea],
  components: { SupplierTabsForm, },
  data () {
    const supplierBill = getInitialFieldsValue([
      PRODUCT_INFORMATION_FIELDS,
      MANAGEMENT_INFORMATION_FIELDS,
      DELIVERY_INFORMATION_FIELDS,
    ])
    const supplierContractBill = getInitialFieldsValue([
      BILLING_INFORMATION_FIELDS,
      BILLING_METHOD_FIELDS,
      FINANCIAL_INFORMATION_FIELDS,
    ])
    supplierBill.mainCategoryId = ''
    supplierBill.managementAreaName = ''
    const supplierPayeeBillList = []
    const supplierPersonBillList = []
    const supplierSettlementBillList = []
    return {
      loading: false,
      data: {},
      supplierBill,
      supplierContractBill,
      supplierPayeeBillList,
      supplierPersonBillList,
      supplierSettlementBillList,
      dialogVisible: false,
      causeDescription: '',
      supplierInfoList: [{
        name: '人员信息',
        children: [{
          ref: 'supplier-staff-list',
          type: 'staffList',
          dataSource: ['supplierPersonBillList'],
          rules: [{}]
        }]
      }, {
        name: '商品信息',
        children: [{
          name: '商品信息',
          ref: 'product-information-form',
          formFields: PRODUCT_INFORMATION_FIELDS,
          dataSource: 'supplierBill',
        }],
      }, {
        name: '管理信息',
        children: [{
          name: '管理信息',
          ref: 'management-information-form',
          formFields: MANAGEMENT_INFORMATION_FIELDS.filter(item => {
            return !['supplierCode', 'createUser', 'createTime'].includes(item.prop)
          }),
          dataSource: 'supplierBill',
        }],
      }, {
        name: '合同信息',
        children: [{
          name: '开户行信息',
          ref: 'bank-information-list',
          type: 'bankList',
          dataSource: 'supplierPayeeBillList',
          rules: {
            invoiceSupplierName: [{ required: true, message: '请关联开票供应商' }],
          }
        }, {
          name: '结算信息',
          ref: 'billing-information-form',
          formFields: BILLING_INFORMATION_FIELDS,
          dataSource: 'supplierContractBill',
        }, {
          name: '结算方式',
          ref: 'settlement-method-list',
          type: 'settlementMethod',
          dataSource: 'supplierSettlementBillList',
        }, {
          name: '',
          ref: 'billing-method-form',
          formFields: BILLING_METHOD_FIELDS,
          dataSource: 'supplierContractBill',
        }, {
          name: '财务信息',
          ref: 'financial-information-form',
          formFields: FINANCIAL_INFORMATION_FIELDS,
          dataSource: 'supplierContractBill',
        }, {
          name: '交付信息',
          ref: 'delivery-information-form',
          formFields: DELIVERY_INFORMATION_FIELDS,
          dataSource: 'supplierBill',
        }],
      }],
      rules: {
        supplierType: [{ required: true, message: '请选择供应商类型', trigger: 'blur' }],
        supplierName: [{ required: true, message: '请输入供应商名称', trigger: 'blur' }],
      },
      supplierData: {
        supplierBill,
        supplierContractBill,
        supplierPayeeBillList,
        supplierPersonBillList,
        supplierSettlementBillList,
      }
    }
  },
  methods: {
    // 检查表单 获取数据
    checkValidity () {
      this.$refs['supplier-form'].getData(1).then((data) => {
        if (data) {
          this.data = {
            specialSupplierBase: data.supplierBill,
            specialSupplierContract: data.supplierContractBill,
            specialSupplierPayeeList: data.supplierPayeeBillList,
            specialSupplierPeople: data.supplierPersonBillList,
            supplierSettlementBillList: data.supplierSettlementBillList,
          }
          this.dialogVisible = true
        }
      })
    },
    // 提交
    handleSubmit () {
      if (!this.causeDescription) {
        this.$message.error('请输入原因说明')
        return
      }
      /**
       * 状态, 1：暂存 10：待审核，20：审核通过，30：审核驳回, 40: 撤回
       */
      /**
       * 1：供应商新增，2：供应商编辑，3：线索转潜在，4：线索转合格，5：潜在转合格，6：加入黑名单，7：移出黑名单
       * ，8：合格降级潜在 9：潜在降级线索，10：淘汰供应商，11：激活供应商，12：运费新增，13：运费编辑  14:考察计划  15:考察报告
       * 16: 潜在供应商新增 17:潜在供应商修改  18:更换业务对接人  19:特殊供应商新增 20:特殊供应商编辑
       */
      const obj = {
        processDocumentStatus: 10,
        processDocumentOperateType: 19,
        causeDescription: this.causeDescription
      }
      console.log(`processDocumentOperateType: ${obj.processDocumentOperateType}; processDocumentStatus: ${obj.processDocumentStatus}`)
      // console.log('data :>> ', Object.assign(obj, this.data));
      this.addSupplierDetail(Object.assign(obj, this.data)).catch((reason) => {
        console.log('reason', reason)
        this.loading = false
      })
    },
    // 新增供应商
    async addSupplierDetail (data) {
      // return
      this.loading = true;
      const resp = await addSpecialSupplierBillService(data)
      this.loading = false;
      if (resp && resp.status === '0') {
        this.$message.success('保存成功')
        // this.handleBack()
        this.$router.push({ name: 'pendingApprovalList' })
      }
    },
    handleBack () {
      this.$router.go(-1)
    },
  },
}
</script>
